package net.xdclass.service.impl;

import lombok.extern.slf4j.Slf4j;
import net.xdclass.common.LoginUser;
import net.xdclass.controller.request.AccountLoginRequest;
import net.xdclass.controller.request.AccountRegisterRequest;
import net.xdclass.enums.AuthTypeEnum;
import net.xdclass.enums.BizCodeEnum;
import net.xdclass.enums.SendCodeEnum;
import net.xdclass.manager.AccountManager;
import net.xdclass.model.AccountDO;
import net.xdclass.mapper.AccountMapper;
import net.xdclass.service.AccountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.xdclass.service.NotifyService;
import net.xdclass.utils.CommonUtil;
import net.xdclass.utils.JWTUtil;
import net.xdclass.utils.JsonData;
import org.apache.commons.codec.digest.Md5Crypt;
import org.apache.commons.lang3.StringUtils;
import org.bouncycastle.jcajce.provider.digest.MD5;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 手机验证码
 * 密码加密
 * 账号唯一性检查
 * 插入数据库
 * 新用户福利猪测发放
 * @author 二当家小D
 * @since 2025-02-20
 */
@Service
@Slf4j
public class AccountServiceImpl extends ServiceImpl<AccountMapper, AccountDO> implements AccountService {


    @Autowired
    private NotifyService notifyService;

    @Autowired
    private AccountManager accountManager;
    @Override
    public JsonData register(AccountRegisterRequest accountRegisterRequest) {

        //判断验证码是否正确
        boolean checkCode = false;
        //先验证手机是否为空
        if (StringUtils.isNotBlank(accountRegisterRequest.getPhone())) {
            checkCode = notifyService.checkCode(SendCodeEnum.USER_REGISTER, accountRegisterRequest.getPhone(), accountRegisterRequest.getCode());
        }
        //验证码错误
        if (!checkCode) {
            return JsonData.buildResult(BizCodeEnum.CODE_ERROR);
        }
        AccountDO accountDO = new AccountDO();
        //生成唯一的账号
        accountDO.setAccountNo(CommonUtil.getCurrentTimestamp());
        //拷贝属性
        BeanUtils.copyProperties(accountRegisterRequest, accountDO);
        //认证几把
        accountDO.setAuth(AuthTypeEnum.DEFAULT.name());
        //密码设置 秘钥 盐
        accountDO.setSecret("$1$" + CommonUtil.getStringNumRandom(8));
        //秘钥
        String cryptPwd = Md5Crypt.md5Crypt(accountRegisterRequest.getPwd().getBytes(),accountDO.getSecret());
        //设置密码
        accountDO.setPwd(cryptPwd);
        //插入数据库
        int rows = accountManager.insert(accountDO);
        log.info("rows:{}", "注册成功:{}", rows, accountDO);

        //用户初始化发放福利 TODO
        userRegisterInitTask(accountDO);
        return JsonData.buildSuccess();
    }

    @Override
    public JsonData login(AccountLoginRequest accountLoginRequest) {
        List<AccountDO> accountDOList = accountManager.findByPhone(accountLoginRequest.getPhone());

        if (accountDOList != null && accountDOList.size() > 0) {
            //获取加密的内容
            AccountDO accountDO = accountDOList.get(0);
            String cryptPwd = Md5Crypt.md5Crypt(accountLoginRequest.getPwd().getBytes(), accountDO.getSecret());
            if (cryptPwd.equalsIgnoreCase(accountDO.getPwd())) {
                LoginUser loginUser = LoginUser.builder().build();
                BeanUtils.copyProperties(accountDO, loginUser);

                String token = JWTUtil.createJWTToken(loginUser);
                //生成JWT
                return JsonData.buildSuccess(token);
            }else {
                //密码错误
                return JsonData.buildResult(BizCodeEnum.ACCOUNT_PWD_ERROR);
            }

        }else {
            return JsonData.buildResult(BizCodeEnum.ACCOUNT_UNREGISTER);
        }

    }


    /**
     * 用户初始化 流量包a
     * @param accountDO
     */
    private void userRegisterInitTask(AccountDO accountDO) {

    }
}
